#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
using std::cerr;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::ifstream;
using std::ofstream;
using std::istringstream;


class Dictionary
{
public:
    void read(const string &filename){
        ifstream ifs(filename);
        
        if(!ifs){
            cerr << "ifstream open file failed!" << endl;
            return;
        }

        string line;
        while(getline(ifs,line)){
            istringstream iss(line);
            string word;
            while(iss >> word){
                //处理单词
                dealWord(word);

                //插入到map
                if(word != string()){
                    ++_dict[word];
                }
            }
        }

        ifs.close();
    }

    void dealWord(string & word){
        auto it = word.begin();
        while(it != word.end()){
            if(!isalpha(*it)){
                it = word.erase(it);
            }else{
                ++it;
            }
        }
    }
   
    void store(const string &filename){
        ofstream ofs(filename);
        if(!ofs){
            cerr << "ofstream open file failed" << endl;
            return;
        }

        map<string,int>::iterator it = _dict.begin();
        for(; it != _dict.end(); ++it){
            ofs << it->first << " " << it->second << endl;
        }

        ofs.close();
    }
private:
    map<string,int> _dict;
};

void test0(){
    Dictionary dictionary;
    dictionary.read("The_Holy_Bible.txt");
    dictionary.store("dictMap.dat");
}

int main(void){
    test0();
    return 0;
}
